from pymongo import MongoClient

client = MongoClient('127.0.0.1', 27017)
# 没有数据库和集合在9行调用的时候会自己加
collection = client['test11']['t311']
# 添加0到999的文档 1000条
info_list = [{'_id': i, 'name': 'py%s' % i} for i in range(1000)]
print(info_list)
# 添加文档
# ret = collection.insert(info_list)
# ret = collection.find({})# 查询_id为100的整数倍的文档
# ret = collection.find({"$where": """
#     function() {
#         var id = 0
#         return this._id > 0 && this._id % 100 == 0;
#     }
#     """})
#
# print(ret)
# print(ret.count())
# for temp in ret:
#     print(temp)
# print(ret)
# for i in ret:
#     print(i)
# 查询_id为100的整数倍的文档
ret = collection.find({"$where": """
    function() {
        var id = 0
        return this._id > 0 && this._id % 100 == 0;
    }
    """})

print(ret)
for temp in ret:
    print(temp)